package model

import (
	"fmt"
	"gorm.io/gorm"
	"time"
)

type User struct {
	gorm.Model
	ID        int64     `json:"id"`
	Phone     string    `json:"phone"`
	PassWord  string    `json:"pass_word"`
	Token     string    `json:"token"`
	CreatedAt time.Time `json:"created_at,string"`
	UpdatedAt time.Time `json:"updated_at,string"`
}

//func Login( phone string,passWord string) error {
//	return db.Row(`select 'id' from tb_users where phone =? and password=?`, phone, passWord).Error
//}

func QueryUserId(phone string) (User, error) {
	user := User{}
	return user, db.Where("phone = ?", phone).First(&user).Error
}

func QueryUserToken(token string) (User, error) {
	user := User{}
	return user, db.Where("token = ?", token).First(&user).Error
}

func UpUserToken(id int64, token string) error {
	fmt.Println(id, token)
	return db.Model(&User{}).Where("id = ?", id).Update("token", token).Error
}

func QueryUser(userId int64) (User, error) {
	fmt.Printf("查询时候的ID d%", userId)
	user := User{}
	return user, db.First(&user, userId).Error
}
func Register(phone string, passWord string) error {
	user := User{
		Phone:    phone,
		PassWord: passWord,
	}
	return db.Create(&user).Error
}
